package com.device.operating.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.device.operating.dao.UserRepository;
import com.device.operating.entity.Department;
import com.device.operating.entity.User;
import com.device.operating.service.DepartmentService;
import com.device.operating.service.UserService;

@Service("userService")
@Transactional(propagation=Propagation.REQUIRED)
public class UserServiceImpl implements UserService{

	@Autowired
	private UserRepository UserRepository;
	
	@Resource(name="DepartmentService")
	private DepartmentService DepartmentService;
	
	@Override
	public void addUser(User user) {
		UserRepository.save(user);
	}
	
	public User login(String tel,String password){
		return UserRepository.findByTelAndPassword(tel, password);
	}
	
	
	public User queryUser(String userId){
		return UserRepository.findOne(userId);
	}

	@Override
	public List<User> querybyname(String username) {
		// TODO Auto-generated method stub
		return UserRepository.findByUsername(username);
	}

	@Override
	public Map queryalluserbydepartmentId(String departmentId, int fr, int mr,boolean flag) {
		// TODO Auto-generated method stub
		Map map=new HashMap<>();
		int count =0;
		List<User> list=newcreatchild(departmentId);
		int total=list.size();
		if(flag==false){//直接取出所有结果
			map.put("rows", list);
			map.put("total", total);
			return map;
		}
		Collections.sort(list,new Comparator<User>(){
		public int compare(User arg0, User arg1) {
			      return arg0.getRolelevel().compareTo(arg1.getRolelevel());
			  }
		});
		int max=fr*mr+mr;
		if(total<max)
		max=total;
		list=list.subList(fr*mr, max);
		map.put("rows", list);
		map.put("total", total);
		return map;
	}
	
	public List<User> creatchild(List<User> listuser,List<String> listdpId){
		if(listdpId.size()==0){
			return listuser;
		}
		List<String> newlistdpId=new ArrayList<String>();
		for(String dpId:listdpId){
			listuser.addAll(UserRepository.findByDepartmentId(dpId));
			List<Department> list=DepartmentService.querybyparentId(dpId);
			for(Department dp:list){
				String childdpId=dp.getDepartmentId();
				//listuser.addAll(UserRepository.findByDepartmentId(childdpId));
				newlistdpId.add(childdpId);
			}
		}
		this.creatchild(listuser, newlistdpId);
		return listuser;
	}
	
	public List<User> newcreatchild(String departmentId){
		List<User> listuser=new ArrayList<>();
		List<String> listdpId=new ArrayList<>();
		listdpId.add(departmentId);
		while(true){
			if(listdpId.size()==0){
				break;
			}
			List<String> ls=new ArrayList<>();
			for(String dpId:listdpId){
				List<Department> list=DepartmentService.querybyparentId(dpId);
				listuser.addAll(UserRepository.findByDepartmentId(dpId));
				for(Department dp:list){
					String childdpId=dp.getDepartmentId();
					ls.add(childdpId);
				}
			}
			listdpId.clear();
			listdpId.addAll(ls);
		}
		return listuser;
	}
	
	@Override
	public List<User> querybydepartmentId(String dpId) {
		// TODO Auto-generated method stub
		return UserRepository.findByDepartmentId(dpId);
	}

	@Override
	public List<User> queryByRootdpId(String rootdpId) {
		// TODO Auto-generated method stub
		return UserRepository.findByRootdpId(rootdpId);
	}

	@Override
	public void deleteuser(String userId) {
		// TODO Auto-generated method stub
		UserRepository.delete(userId);
	}

	@Override
	public List<User> querybyusercode(String usercode) {
		// TODO Auto-generated method stub
		return UserRepository.findByUsercode(usercode);
	}
	
}
